package org.gmoting.exif;

import static com.drew.imaging.jpeg.JpegMetadataReader.readMetadata;
import static org.gmoting.util.Util.EXT_JPG_MAY;
import static org.gmoting.util.Util.EXT_JPG_MIN;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;

import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.MetadataException;
import com.drew.metadata.Tag;

public class Main {

	private final static String fichero = Messages
			.getString("main.fichero.entrada");
	private final static String directorio = Messages
			.getString("main.directorio.entrada");
	private final static String directorioSalida = Messages
			.getString("main.fichero.salida");

	private final static FileFilter fileFilter = new FileFilter() {
		public boolean accept(File file) {
			return file.getName().endsWith(EXT_JPG_MAY)
					|| file.getName().endsWith(EXT_JPG_MIN);
		}
	};

	public static void main(String[] args) {
		try {
			BufferedWriter ficheroSalida = new BufferedWriter(new FileWriter(
					Messages.getString("main.fichero.salida")));
			File dir = new File(directorio);

			// Filtrar solo las extensiones .jpg y .JPG
			File files[] = dir.listFiles(fileFilter);

			for (File file : files) {
				if (file.isFile()) {
					// addGPSExif(file,"");
					ficheroSalida.write(readEXIFMetadaExtractor(file)
							.toString());
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Leer EXIF con Metadata Extractor
	 * 
	 * @param filename
	 *            Ruta de la imagen en formato JPG
	 * @return Etiquetas exif
	 * @throws IOException
	 * @throws JpegProcessingException
	 * @throws MetadataException
	 */
	@SuppressWarnings("unchecked")
	public static StringBuilder readEXIFMetadaExtractor(File file)
			throws IOException, JpegProcessingException, MetadataException {
		StringBuilder imageTags = new StringBuilder();
		imageTags.append(file.getAbsolutePath() + "\n");

		Metadata metadata = readMetadata(file);
		// iterate through metadata directories
		Iterator directories = metadata.getDirectoryIterator();
		while (directories.hasNext()) {
			Directory directory = (Directory) directories.next();
			// iterate through tags and print to System.out
			Iterator tags = directory.getTagIterator();
			while (tags.hasNext()) {
				Tag tag = (Tag) tags.next();
				imageTags.append(tag + "\n");
				System.out.println(tag);
			}
		}
		imageTags.append("\n\n");
		return imageTags;
	}
}
